#include "App.h"
#include "DebugTracer.h"

extern DebugTracer _DEBUGTRACER;
App::App()
{
    m_game = 0;
    m_gameView = 0;
}

App::~App()
{
    delete OgreFramework::getSingletonPtr();
}


void App::Start()
{
	new OgreFramework();
    if(!OgreFramework::getSingletonPtr()->initOgre("SpeedMania v1.0", this, 0))
        return;
	
	m_bShutdown = false;
	
	OgreFramework::getSingletonPtr()->m_pLog->logMessage("SpeedMania initialized!");
	
	//m_sceneManager->setAmbientLight(Ogre::ColourValue(1.0f,1.0f,1.0f));
	
    m_game = new Game(OgreFramework::getSingletonPtr()->m_pSceneMgr->getRootSceneNode());
	
    m_gameView = new GameView(OgreFramework::getSingletonPtr()->m_pRenderWnd,OgreFramework::getSingletonPtr()->m_pSceneMgr,m_game);
    
	/*#ifdef _DEBUG
    m_root = new Ogre::Root("plugins_d.cfg","ogre.cfg","ogre.log");
    #else
    m_root = new Ogre::Root("plugins.cfg","ogre.cfg","ogre.log");
    #endif
    Ogre::ConfigFile configFile;
    configFile.load("resources.cfg");
    Ogre::ConfigFile::SectionIterator seci = configFile.getSectionIterator();
    Ogre::String secName, typeName, archName;
    while(seci.hasMoreElements())
    {
        secName = seci.peekNextKey();
        Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
        Ogre::ConfigFile::SettingsMultiMap::iterator i;
        for(i = settings->begin(); i != settings->end(); ++i)
        {
            typeName = i->first;
            archName = i->second;
            Ogre::ResourceGroupManager::getSingleton().addResourceLocation(archName,typeName,secName);
        }
    }
    Ogre::RenderSystem *rs = m_root->getRenderSystemByName("Direct3D9 Rendering Subsystem"); // Pour OpenGL : "OpenGL Rendering Subsystem"
    m_root->setRenderSystem(rs);
    rs->setConfigOption("Full Screen", "No");
    rs->setConfigOption("Video Mode", "800 x 600 @ 32-bit colour");
    rs->setConfigOption("VSync", "Yes");

    m_window = m_root->initialise(true, "test_ogre");
    Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
    Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();

    m_sceneManager = m_root->createSceneManager("DefaultSceneManager","My scene manager");*/

    //TEST
    
	

}

void App::Run()
{
	OgreFramework::getSingletonPtr()->m_pLog->logMessage("Start main loop...");
	
	double timeSinceLastFrame = 0;
	double startTime = 0;
	
	OgreFramework::getSingletonPtr()->m_pRenderWnd->resetStatistics();
	
	while(!m_bShutdown && !OgreFramework::getSingletonPtr()->isOgreToBeShutDown()) 
	{
		if(OgreFramework::getSingletonPtr()->m_pRenderWnd->isClosed())m_bShutdown = true;
		
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
		Ogre::WindowEventUtilities::messagePump();
#endif	
		if(OgreFramework::getSingletonPtr()->m_pRenderWnd->isActive())
		{
			startTime = OgreFramework::getSingletonPtr()->m_pTimer->getMillisecondsCPU();
			Render(timeSinceLastFrame);
			timeSinceLastFrame = OgreFramework::getSingletonPtr()->m_pTimer->getMillisecondsCPU() - startTime;
		}
		else
		{
			std::cout << "Sleep" << std::endl;
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
			Sleep(1000);
#elif OGRE_PLATFORM == OGRE_PLATFORM_APPLE
			sleep(1000);
#endif
		}
	}
	
	OgreFramework::getSingletonPtr()->m_pLog->logMessage("Main loop quit");
	OgreFramework::getSingletonPtr()->m_pLog->logMessage("Shutdown OGRE...");
}

void App::Render(double timeSinceLastFrame){
	
	
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
	OgreFramework::getSingletonPtr()->m_pKeyboard->capture();
#endif
	OgreFramework::getSingletonPtr()->m_pMouse->capture();
	
	m_gameView->Update(timeSinceLastFrame);
	m_game->Update(timeSinceLastFrame);
	
	OgreFramework::getSingletonPtr()->updateOgre(timeSinceLastFrame);
	if(!OgreFramework::getSingletonPtr()->m_pRoot->renderOneFrame())
		m_bShutdown = true;
	
	
}


bool App::keyPressed(const OIS::KeyEvent &keyEventRef)
{
#if OGRE_PLATFORM != OGRE_PLATFORM_IPHONE
	OgreFramework::getSingletonPtr()->keyPressed(keyEventRef);
	
	if(OgreFramework::getSingletonPtr()->m_pKeyboard->isKeyDown(OIS::KC_F))
	{
		//do something
	}
#endif
	return true;
}

//|||||||||||||||||||||||||||||||||||||||||||||||

bool App::keyReleased(const OIS::KeyEvent &keyEventRef)
{
	OgreFramework::getSingletonPtr()->keyReleased(keyEventRef);
	
	return true;
}

void App::Accelerated(double x, double y, double z, double timeSinceLastFrame)
{
	float range = .3f;
	if(y < range)
		m_game->m_player->m_jet->
	
}
